<?php
/**
 * Created by PhpStorm
 * User: lcz
 * Date: 2019/9/10
 * Time: 22:26
 * 访问记录
 */
namespace app\api\controller;

use app\common\controller\Api;
use Symfony\Component\HttpFoundation\Tests\StringableObject;

class Visible extends Api {
	protected $noNeedLogin = ['*'];
	
	/**
	 * 进入
	 */
	public function enter(){
		$model = new \app\common\model\Visible();
		$data = $this->request->post();
		$data['params'] = isset($data['params']) ? json_encode($data['params']): '';
		$res = $model->isUpdate(false)
			->allowField(true)
			->save($data);
		if($res) {
			$this->success('记录成功', ['id' => $model->getLastInsID()]);
		} else {
			$this->error('记录出错');
		}
	}
	
	/**
	 * 离开
	 */
	public function leave(){
		$id = $this->request->post('id');
		!$id && $this->error('参数错误');
		
		$model = new \app\common\model\Visible();
		$row = $model->find($id);
		!$row && $this->error('记录不存在');
		
		$res = $model->save(['duration' => time()-$row['create_time'], 'leave_time' => time()], ['id' => $id]);
		if($res !== false) {
			$this->success('记录成功');
		} else {
			$this->error('记录出错');
		}
	}
}
